package team.star.werespos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.star.werespos.exception.GlobalException;
import team.star.werespos.mapper.FoodCategoryMapper;
import team.star.werespos.model.entity.FoodCategory;
import team.star.werespos.model.enums.CodeMsgEnum;
import team.star.werespos.service.FoodCategoryService;

import java.time.LocalTime;
import java.util.List;
import java.util.Optional;

/**
 * FoodCategoryServiceImpl class<br/>
 *
 * @author hdonghong
 * @since 2019/03/23
 */
@Slf4j
@Service
public class FoodCategoryServiceImpl implements FoodCategoryService {

    @Autowired
    private FoodCategoryMapper foodCategoryMapper;

    @Override
    public void insertOne(FoodCategory foodCategory) {
        // 非空检查
        Preconditions.checkNotNull(foodCategory, "foodCategory is null!");
        // 新增
        Optional.ofNullable(foodCategoryMapper.insert(foodCategory))
                .ifPresent(result -> {
                    if (result == 0) {
                        log.error("【新增商品类型】失败！result = [{}], foodCategory = [{}]",
                                result, foodCategory);
                        throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
                    }
                });
    }

    @Override
    public List<FoodCategory> listCategory(boolean flag) {
        LocalTime nowTime = LocalTime.now();
        String timeFoodCatetory = null;
        // 6-10 为早餐供应时间；10.30 - 14为午餐供应时间；14-16为下午茶； 16 - 20为晚餐供应时间；20 - 2为宵夜时间
        if (nowTime.isAfter(LocalTime.of(5, 0)) && nowTime.isBefore(LocalTime.of(9, 0))) {
            timeFoodCatetory = "早餐";
        } else if (nowTime.isAfter(LocalTime.of(9, 30)) && nowTime.isBefore(LocalTime.of(13, 0))) {
            timeFoodCatetory = "午餐";
        } else if (nowTime.isAfter(LocalTime.of(13, 0)) && nowTime.isBefore(LocalTime.of(16, 0))){
            timeFoodCatetory = "下午茶";
        }  else if (nowTime.isAfter(LocalTime.of(16, 0)) && nowTime.isBefore(LocalTime.of(19, 0))){
            timeFoodCatetory = "晚餐";
        } else {
            timeFoodCatetory = "宵夜";
        }
        QueryWrapper<FoodCategory> queryWrapper = new QueryWrapper<FoodCategory>()
                .orderByAsc("create_time");
        if (flag)
            queryWrapper.notIn("category_name", "早餐", "午餐", "晚餐", "宵夜", "下午茶");
        List<FoodCategory> categoryList = foodCategoryMapper.selectList(
                queryWrapper
        );
        if (flag) {
            FoodCategory timeFoodCate = foodCategoryMapper.selectOne(
                    new QueryWrapper<FoodCategory>().eq("category_name", timeFoodCatetory)
            );
            categoryList.add(timeFoodCate);
        }



        return categoryList;
    }

    @Override
    public void updateById(FoodCategory foodCategory) {
        Optional.ofNullable(foodCategoryMapper.updateById(foodCategory))
                .ifPresent(result -> {
                    if (result == 0) {
                        log.error("【修改商品类型】失败！result = [{}], foodCategory = [{}]",
                                result, foodCategory);
                        throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
                    }
                });
    }

    @Override
    public void deleteById(FoodCategory foodCategory) {
        Optional.ofNullable(foodCategoryMapper.deleteById(foodCategory))
                .ifPresent(result -> {
                    if (result == 0) {
                        log.error("【删除商品类型】失败！result = [{}], foodCategory = [{}]",
                                result, foodCategory);
                        throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
                    }
                });
    }
}
